package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {
    @Reference(version = "1.0.0")
    private SetMealService setMealService;
    @Autowired
    private JedisPool jedisPool;


    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        if (queryPageBean.getQueryString() != null && queryPageBean.getCurrentPage() != 1) {
            queryPageBean.setCurrentPage(1);
        }
        return setMealService.findPage(queryPageBean);
    }


    @PostMapping("/findAll")
    public Result findAll() {
        return setMealService.findAll();
    }

    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal) {
        Result add = setMealService.add(setmeal);

        if(add.isFlag()){
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                jedis.sadd("usePicture", setmeal.getImg());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
        }
        return add;
    }


    @GetMapping("/findById")
    public Result findById(Integer id) {
        return setMealService.findById(id);
    }


    @PostMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            String suffix = StringUtils.substringAfterLast(originalFilename, ".");
            String filename = UUID.randomUUID().toString() + "." + suffix;
            boolean flag = QiniuUtils.upload2Qiniu(file.getBytes(), filename);
            String url = "http://qyymngysq.hb-bkt.clouddn.com/" + filename;
            HashMap<String, String> map = new HashMap<>();
            map.put("url", url);
            map.put("filename", filename);
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                jedis.sadd("allPicture", filename);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
            return Result.success(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.fail(-999, "图片上传失败");
    }


    @GetMapping("/delete")
    public Result delete(Integer id) {
        return setMealService.delete(id);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal) {
        return setMealService.update(setmeal);
    }

}
